package ACWing._851_spfa求最短路;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int m = scanner.nextInt();

        List<List<int[]>> alist = new ArrayList<>(n+1);
        for (int i = 0; i <= n; i++) {
            alist.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {
            int s = scanner.nextInt();
            int e = scanner.nextInt();
            int w = scanner.nextInt();
            alist.get(s).add(new int[]{e,w});
        }

        Queue<Integer> queue = new ArrayDeque<>();
        int[] dist = new int[n+1];
        boolean[] visited = new boolean[n+1];
        Arrays.fill(dist,Integer.MAX_VALUE);
        dist[1] = 0;
        visited[1] = true;  //当前发生过更新的点


        queue.add(1);
        while (!queue.isEmpty()){
            int t = queue.poll();
            visited[t] = false;

            for (int[] edge : alist.get(t)) {
                int b = edge[0],w=edge[1];
                if(dist[b]>dist[t]+w){
                    dist[b] = dist[t]+w;
                    if(!visited[b]) {
                        queue.add(b);
                        visited[b] = true;
                    }
                }
            }
        }
        if(dist[n]==Integer.MAX_VALUE)
            System.out.println("impossible");
        else
            System.out.println(dist[n]);

        scanner.close();
    }
}
